# -*- encoding: utf-8 -*-
from django.test import TestCase
from django.contrib.auth.models import User
from wagglyblog.models import Post, Category

class WagglyBaseTest(TestCase):
    def setUp(self):
        new_admin = User.objects.create_user('admin','admin@localhost.com','admin')
        new_admin.is_superuser = True
        new_admin.is_staff = True
        new_admin.save()
        new_user = User.objects.create_user('user','user@localhost.com','user')
        category = Category('blablub','blablub is a test category','blablub')
        category.save()
        post1 = Post.objects.create(author_id=1, title="First post", text="no need for a lot of text",
            slug='first-post', allow_comments=True, is_published=True)
        post1.categories.add(category)
        post2 = Post.objects.create(author_id=2, title="Second post", text="blablub",
            slug='second-post', allow_comments=False, is_published=True)


class ModelPostTest(WagglyBaseTest):
    def test_create_published_blog(self):
        post = Post.objects.create(author_id=1, text="live", title="...", is_published=True)
        self.assertEquals(Post.objects.all().count(), 3)
        self.assertEquals(Post.published.all().count(), 3)

    def test_create_nonpublished_blog(self):
        post2 = Post.objects.create(author_id=1, text="private", title="blablub", is_published=False)
        self.assertEquals(Post.objects.all().count(), 3)
        self.assertEquals(Post.published.all().count(), 2)


class AddTest(WagglyBaseTest):
    def test_post_db(self):
        posts = Post.objects.all().count()
        self.assertEquals(posts, 2)

    def test_add_before_login(self):
        response = self.client.get('/add/')
        self.failUnlessEqual(response.status_code, 302)

    def test_add_no_permission(self):
        logged_in = self.client.login(username="user", password="user")
        response = self.client.get('/add/')
        self.failUnlessEqual(response.status_code, 302)

    def test_add_after_login(self):
        logged_in = self.client.login(username="admin", password="admin")
        response = self.client.get('/add/')
        self.failUnlessEqual(response.status_code, 200)

    def test_add_post(self):
        self.client.login(username="admin", password="admin")
        response = self.client.post('/add/', {'title': 'a new test', 'text': 'my new blogpost',
                                   'allow_comments': True, 'is_published': True})
        self.assertEquals(Post.objects.all().count(), 3)
        new_post = Post.objects.get(pk=3)
        self.assertRedirects(response, "http://testserver%s" % new_post.get_absolute_url())

    def test_add_not_published(self):
        """is_published is supposed to get overridden by add function, thus creating a published blog"""
        self.client.login(username="admin", password="admin")
        response = self.client.post('/add/', {'title': 'a third blog', 'text': 'my third blogpost',
                                   'is_published': False})
        self.assertEquals(Post.objects.all().count(), 3)
        self.assertEquals(Post.published.all().count(), 3)
        self.assertEquals(Post.objects.filter(allow_comments__exact=False).count(), 2)
